package paper.parse;

import paper.token.TT;
import paper.token.Token;

public class Parse {
    public double prob;
    public TT out;
    Token current;
    public Parse next;
    
    Parse(double prob, TT out, Token current) {
        this.prob = prob;
        this.current = current;
        this.out = out;
        next = null;
    }

    static Parse start() {
        return new Parse(1.0, TT.UNKNOWN, null);
    }
    
    public void add(Parse x) {
        next = x;
        prob *= x.prob;
    }

    public void lift(StringBuilder sb, TT x, boolean split) {
        if (out == x || (x == TT.RUBBISH && out != TT.TITLE && out != TT.AUTHOR)) {
            if (split && sb.length() != 0) {
                sb.append("; ");
            }
            if (current != null) {
                sb.append(current.getRawText());
            }
            split = false;
        } else {
            split = true;
        }
        if (next != null) {
            next.lift(sb, x, split);
        }
    }

    public String toString() {
        
        String open="", close="";
        if (out != null) {
            switch (out) {
            case AUTHOR:
                open = "<font color=#d00000>"; close = "</font>"; break;
            case EDITOR:
                open = "<font color=#c000c0>"; close = "</font>"; break;
            case TITLE:
                open = "<font color=#00b000>"; close = "</font>"; break;
            case KEY:
                open = "<font color=#008faf>"; close = "</font>"; break;
            case AFFILIATION:
                open = "<font color=#0000ff>"; close = "</font>"; break;
            case WHERE:
                open = "<tt>"; close = "</tt>"; break;
            case EMAIL:
                open = "<b>"; close = "</b>"; break;
            case NUMBER:
                open = "<em>"; close = "</em>";break;
            case RUBBISH:
                open = "<font color=#8f8f8f>"; close = "</font>"; break;
            case EOF:
                if (current == null && next == null) {
                    return "";
                }
                break;
            default:
                open = "{" + out + ":"; close = "}"; break;
            }
        }

        String debug = " <font size=-4>" + prob + "</font> ";
        String rawText = current == null? "NULL" : current.getRawText();
        String x= open + /* debug + */ rawText + close; 
        if (next != null) {
            x += next;
        }
        return x;
    }
}
